<template>
  <div class="page-container">
    <div>
      <!--工具栏-->
      <div class="toolbar">
        <el-form :inline="true" :model="filters" :size="size" ref="filters">

          <el-row class="search-input">
            <el-form-item label="商品名称：">
              <el-input v-model="filters.chineseName" clearable placeholder="商品名称"></el-input>
            </el-form-item>
            <el-form-item label="商品编码：">
              <el-input v-model="filters.code" clearable placeholder="商品编码"></el-input>
            </el-form-item>
          </el-row>
          <el-row class="search-button">
            <el-form-item>
              <kt-button icon="fa fa-search" :label="$t('action.search')" type="primary" @click="findPage()" />
            </el-form-item>
            <el-form-item>
              <kt-button icon="fa fa-refresh" :label="$t('action.reset')" type="primary" @click="resetForm('filters')" />
            </el-form-item>
            <el-form-item>
              <kt-button icon="fa fa-plus" :label="$t('action.add')" type="primary" @click="handleAdd()" />
            </el-form-item>
          </el-row>
        </el-form>
      </div>
      <!--表格内容栏-->
      <cy-table :height="350" :content="pageResult" :columns="columns"
                :totalSize="totalSize" @findPage="findPage"
                :showEditOperation=false
                :showDeleteOperation=false
                :showOperation=false
                v-loading="loading"
                ref="CyTable"
                :operationWidth="300"
                @selectionChange="selectionChange"
      ></cy-table>
    </div>
  </div>
</template>

<script>
  import CyTable from "@/views/Core/ProductTable";
  import KtButton from "@/views/Core/KtButton";
  export default {
    components: {
      CyTable,
      KtButton,
    },
    data() {
      return {
        selectionChangeList:[],
        loading: true,
        user: JSON.parse(localStorage.getItem("user")),
        dataLifeCycleForm: {
          id: "",
          lifeCycle: ""
        },
        isSaleOptions: [{
          key: 1,
          value: "是"
        }, {
          key: 0,
          value: "否"
        }],
        size: "small",
        brandList: [],
        columns: [
          {
            prop: "code",
            label: "商品编码",
            minWidth: 120
          },
          {
            prop: "chineseName",
            label: "商品名称",
            minWidth: 100
          },
          {
            prop: "fullNamePath",
            label: "商品类目",
            minWidth: 120
          },
          {
            prop: "type",
            label: "商品类型",
            minWidth: 120,
            formatter: this.formatType
          },
          {
            prop: "typeOfProduct",
            label: "商品形式",
            minWidth: 100,
            formatter: this.formatTypeOfProduct
          },

          {
            prop: "salePriceWithTax",
            label: "零售价",
            minWidth: 100
          }
        ],
        pageRequest: {
          pageNum: 1,
          pageSize: 10
        },
        pageResult: [],
        totalSize: 0,
        filters: {
          chineseName: "",
          code: "",
        },
      };
    },
    methods: {

      selectionChange(params) {
        this.selectionChangeList = params.selections;
      },

      // 获取分页数据
      findPage: function (params) {
        let this_ = this;
        if (params == undefined || params == null) {
          params = {
            pageNum: this_.pageRequest.pageNum,
            pageSize: this_.pageRequest.pageSize
          };
        }
        if (this_.filters == undefined || this_.filters == null) {
          this_.filters = {};
        }
        this_.filters.start = params.pageNum;
        this_.filters.limit = params.pageSize;
        this.utils.request.queryPageProductManagement(this_.filters, function (res) {
          if (res.code == "0000") {
            this_.pageResult = res.data.rows;
            this_.totalSize = Number(res.data.total);
            this_.loading = false
          } else {
            res.data.rows = [];
          }
        });
      },
      // 新增
      handleAdd: function() {
        let this_ = this;
        if (this_.selectionChangeList.length < 1) {
          this_.$message({message: "必须选中一条数据", type: "warning"});
          return false;
        }
        if (this_.selectionChangeList.length > 1) {
          this_.$message({message: "只能选中一条数据", type: "warning"});
          return false;
        }
        this_.$refs.filters.validate(valid => {
          if (valid) {
            this_.$confirm("确认提交吗？", "提示", {}).then(() => {
              this_.editLoading = true;
              let params = this_.selectionChangeList[0];
              this.utils.request.saveTopProduct(params, function (res) {
                if (res.code === "0000") {
                  this_.$message({message: "操作成功", type: "success"});
                  this_.$emit("addSuccess");
                } else {
                  this_.$message({message: "操作失败", type: "error"});
                }
              });
            });
          }
        });
      },
      formatType: function (row, column, cellValue, index) {
        if (cellValue === 1) {
          return "实体商品";
        } else if (cellValue === 2) {
          return "虚拟商品";
        } else if (cellValue === 3) {
          return "虚拟组合";
        } else if (cellValue === 4) {
          return "实物组合";
        }
      },
      canSale: function (row, column, cellValue, index) {
        if (cellValue == 0) {
          return "否";
        } else if (cellValue == 1) {
          return "是";
        }
      },
      /*formatStatus: function (row, column, cellValue, index) {
        if (cellValue == 0) {
          return "预审核";
        } else if (cellValue == 1) {
          return "待审核";
        } else if (cellValue == 2) {
          return "审核通过";
        } else if (cellValue == 3) {
          return "审核不通过";
        }
      },*/
      formatTypeOfProduct: function (row, column, cellValue, index) {
        if (cellValue == 1) {
          return "普通商品";
        } else if (cellValue == 2) {
          return "系类虚品";
        }
      },
      formatSource: function (row, column, cellValue, index) {
        if (cellValue == 1) {
          return "平台";
        } else if (cellValue == 2) {
          return "商家";
        }
      },
      resetForm(formName) {
        this.filters = {
          chineseName: "",
          code: "",
          thirdProductCode: "",
          brandId: "",
          categoryId: "",
          combineType: "",
          canSale: ""
        };
        this.findPage();
      },
      handTabChange(param) {
        if (param == 1) {
          this.filters.canSale = "";
          this.filters.status = "";
          this.findPage();
        } else if (param == 4) {
          this.filters.canSale = "";
          this.filters.status = "0";
          this.findPage();
        } else if (param == 6) {
          this.filters.canSale = "";
          this.filters.status = "2";
          this.findPage();
        }
      },
      //初始化页面数据
      queryProductBrand() {
        let _this = this
        if (_this.user.deptType == 0) {
          this.utils.request.queryProductBrand({}, function (data) {
            if (data.data != null) {
              _this.brandList = data.data
            }
          })
        } else {
          this.utils.request.queryMerchantProductBrand({}, function (data) {
            if (data.data != null) {
              _this.brandList = data.data
            }
          })
        }
      },
    },
    mounted() {
      this.queryProductBrand();
    }
  };
</script>

<style scoped lang="scss">
</style>
